﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.ProgressBar
{
	public partial class ProgressBarForm : System.Windows.Forms.Form
	{
		public ProgressBarForm(int length, ProgressBarStyle style = ProgressBarStyle.Blocks, bool bShowCancel = true)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			Application.EnableVisualStyles();
			base.Shown += this.Form1_Shown;
			this.backgroundWorker.WorkerReportsProgress = true;
			this.progressBar.Value = 0;
			this.progressBar.Maximum = length;
			this.m_ProgressBarStyle = style;
			this.m_ShowCancel = bShowCancel;
		}

		public ProgressBarForm(int length, string strTitle, ProgressBarStyle style = ProgressBarStyle.Blocks, bool bShowCancel = true, bool bShowInTaskBar = false, bool bIsChangeIcon = false)
		{
			this.InitializeComponent();
			if (bIsChangeIcon)
			{
				base.Icon = Resource.LonghuIcon;
			}
			Application.EnableVisualStyles();
			base.Shown += this.Form1_Shown;
			this.backgroundWorker.WorkerReportsProgress = true;
			this.progressBar.Value = 0;
			this.progressBar.Maximum = length;
			this.m_ProgressBarStyle = style;
			this.m_ShowCancel = bShowCancel;
			this.Text = strTitle;
			base.ShowInTaskbar = bShowInTaskBar;
		}

		public void SetNoneCancle()
		{
			base.ControlBox = false;
			this.btnCancel.Visible = false;
		}

		public void SetEscDisable(bool bDisable = true)
		{
			this.m_bEscDisable = bDisable;
		}

		public void SetMaxValue(int newLength)
		{
			this.progressBar.Value = 0;
			this.progressBar.Maximum = newLength;
		}

		private void Form1_Shown(object sender, EventArgs e)
		{
			this.backgroundWorker.RunWorkerAsync();
		}

		public void RefreshProgressBar()
		{
			if (this.m_ProgressBarStyle == ProgressBarStyle.Marquee)
			{
				this.m_stopTag = false;
				base.Dispose();
				return;
			}
			ProgressBar progressBar = this.progressBar;
			int value = progressBar.Value;
			progressBar.Value = value + 1;
			if (this.progressBar.Value < this.progressBar.Maximum)
			{
				Application.DoEvents();
				return;
			}
			this.m_stopTag = false;
			base.Dispose();
		}

		private void ProgressBarForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (this.m_bEscDisable)
			{
				return;
			}
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void ProgressBarForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (!base.ControlBox)
			{
				return;
			}
			if (YJKMessageBox.Show("你确定终止吗", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) != DialogResult.Yes)
			{
				this.m_stopTag = false;
				e.Cancel = true;
				return;
			}
			this.m_stopTag = true;
			this.progressBar.Dispose();
		}

		private void ProgressBarForm_Load(object sender, EventArgs e)
		{
			this.progressBar.Style = this.m_ProgressBarStyle;
			this.btnCancel.Visible = this.m_ShowCancel;
			this.progressBar.MarqueeAnimationSpeed = 40;
		}

		private BackgroundWorker backgroundWorker = new BackgroundWorker();

		public bool m_stopTag;

		private ProgressBarStyle m_ProgressBarStyle;

		private bool m_ShowCancel = true;

		private bool m_bEscDisable;
	}
}
